﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

namespace mal.balades.Web
{
    public partial class GetImage : IHttpHandler
    {
        private const string ImageUrlKey = "url";
        private const string ClearCacheKey = "clear";
        private HttpContext context;

        public void ProcessRequest(HttpContext context)
        {
            this.context = context;

            string imageUrl;
            byte[] image;

            if (context.Request[ImageUrlKey] != null)
            {
                imageUrl = context.Request[ImageUrlKey];

                WebClient webClient = new WebClient();

                image = webClient.DownloadData(new Uri(imageUrl, UriKind.Absolute));


                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(image);

            }
        }        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public static string GetUrl(string url)
        {
            return string.Format("http://localhost:1720/GetImage.ashx?{0}={1}", ImageUrlKey, url);
        }
    }
}